const customRequst = require('../../../common/customRequst');
let app = getApp();
Page({
  data: {
    orderId: '',
    Formtoken: '',
    orderInfo: {},
    isSubmit: false,
    uploadFileObj: {
      url: '',
      filename: '',
      fullurl: '',
      isSuccess: false
    },
  },
  onLoad: function (options) {

    let that = this
    if (options.id) {
      that.setData({
        orderId: options.id
      })
    }
    //获取订单信息
    customRequst.postRequest('api/contribution/getfile', {
      id: that.data.orderId
    }).then(function (res) {
      if (res.code == 1) {
        //初始化订单旧文件
        let uploadFileObj = that.data.uploadFileObj;
        uploadFileObj.url = res.data.file_path,
        uploadFileObj.filename = res.data.file_name?res.data.file_name: res.data.file_path,
        uploadFileObj.fullurl = res.data.file_path_all,
        uploadFileObj.isSuccess = true,

        that.setData({
          orderInfo: res.data,
          uploadFileObj:uploadFileObj
        })
        
      } else {
        that.isSubmit = true;
        wx.showToast({
          title: res.msg,
        });
      }
    })
    customRequst.postRequest('api/common/get_form_token', {}).then(res => {
      if (res.code == 1) {
        that.setData({
          formToken: res.data.token
        })
      } else {
        wx.showToast({
          title: res.msg,
        })
      }
    })
  },

  onShow: function () {
    let that = this
    //监听是否上传了文件
    let gUploadFilObj = app.globalData.uploadFileObj;
    if (gUploadFilObj) {
      let uploadFileObj = that.data.uploadFileObj;
      uploadFileObj.url = gUploadFilObj.url;
      uploadFileObj.filename = gUploadFilObj.filename;
      uploadFileObj.fullurl = gUploadFilObj.fullurl;
      uploadFileObj.isSuccess = true;
      that.setData({
        uploadFileObj: uploadFileObj
      })
    }
  },

  //监听页面销毁
  onUnload: function () {
    //清理文件
    app.globalData.uploadFileObj = "";
  },

  //h5上传文件
  uploadFile: function () {
    let h5UploadUrl = app.globalData.workAipDomain + '/api/webupload/index';
    h5UploadUrl = h5UploadUrl.replace(/http:/, 'https:')
    wx.navigateTo({
      url: '/pages/contribute/uploadfile/index?url=' + h5UploadUrl,
    })
  },

  //删除文件
  deleteFile: function () {
    let that = this;
    let uploadFileObj = that.data.uploadFileObj;
    uploadFileObj = {
        url: '',
        filename: '',
        fullurl: '',
        isSuccess: false
      },
      that.setData({
        uploadFileObj: uploadFileObj
      });
    //清理文件
    app.globalData.uploadFileObj = "";
  },


  //数据提交
  formSubmit: function (e) {

    let that = this
    if (that.data.isSubmit) {
      wx.showToast({
        title: '请勿重复提交',
        icon: 'none'
      })
      return;
    }
    let works_name = e.detail.value.works_name
    if (!works_name) {
      wx.showToast({
        title: '文章名称不能为空',
        icon: 'none'
      })
      return;
    }
    let author = e.detail.value.author
    if (!author) {
      wx.showToast({
        title: '作者不能为空',
        icon: 'none'
      })
      return;
    }
    if (!that.data.uploadFileObj.url) {
      wx.showToast({
        title: '文件不能为空',
        icon: 'none'
      })
      return;
    }

    //判断是否修改过文件
    let upload_file_status = 0;
    if(that.data.orderInfo.file_path != that.data.uploadFileObj.url){
      upload_file_status = 1;
    }

    that.setData({
      isSubmit: true
    })
    customRequst.postRequest('api/contribution/setfile', {
      id: that.data.orderId,
      token: that.data.formToken,
      file_path: that.data.uploadFileObj.url,
      filename:that.data.uploadFileObj.filename,
      upload_file_status: upload_file_status,
      works_name: works_name,
      author: author,
    }).then(res => {
      if (res.code == 1) {
        //清理文件
        app.globalData.uploadFileObj = "";
        wx.showToast({
          title: '修改信息成功',
          icon: 'success',
          duration: 2000,
          mask: true
        });
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/order/list/index',
          })
        }, 2000);
      } else {
        wx.showToast({
          title: res.msg,
          mask: true,
          success: function () {
            that.setData({
              isSubmit: false,
              Formtoken: res.data.token
            });
          }
        });
      }
    })
  },

})